# -*- encoding=utf8 -*-
__author__ = "Bryan.Chen"

from airtest.core.api import *

auto_setup(__file__)

from poco.drivers.std import StdPoco
poco = StdPoco()

using("../FlowTest.air")
import FlowTest

class MahjongPlay(FlowTest.BaseTestCase):

    def __init__(self):
        print("%s __init__" % (__file__))
    
    def runTest(self, args):
        
        poco("TableScene").wait_for_appearance()
        
        isPlaying = True
        
        while isPlaying:
            
            optLayer = poco("OptSelectListLayer")
            optList = optLayer.offspring("opt_card_list").children()
            
            optLen = len(optList)
            if optLen > 0:
                # 有多个操作
                for i in range(0, len(optList)):
                    if i == (len(optList) - 2):
                        sleep(0.5)
                        optList[i].click([0.5, 0.5])
            else:
                # 无操作，出牌
                myHandcard = poco("MyHandCardLayer").child("Layer").child("hcard_node1")
                myCards = myHandcard.children()
                hszWaitTips = myHandcard.offspring("huan_san_zhang")
                isWaitHsz = hszWaitTips.exists()
                if (not isWaitHsz) and len(myCards) > 0 and (len(myCards) == 5 or (len(myCards) % 2 == 0 and len(myCards) != 4)):
                    sleep(0.5)
                    myCards[len(myCards) - 1].click([0.8, 0.5])
                    myCards[len(myCards) - 1].click([0.8, 0.5])
                    myCards[len(myCards) - 1].click([0.8, 0.5])
                    
            chiGangListSelectLayer = poco("ChiGangListSelectLayer")
            maList = chiGangListSelectLayer.offspring("ma_list2").children()
            if len(maList) > 0:
                for item in maList:
                    item.click([0.5, 0.5])
                    break
                    
            gameOverLayer = poco("YCGameEndLayer")
            if gameOverLayer.exists():
                # 停留2s,再继续下一局
                sleep(2.0) 
                poco("setnumshow_txt").wait_for_appearance()
                txtProgress = gameOverLayer.offspring("setnumshow_txt").get_text()
                rounds = txtProgress.split("/")
                
                btnNext = gameOverLayer.offspring("next_btn")
                btnNext.click([0.5, 0.5])
                
                if rounds[0] < rounds[1]:
                    using("MahjongTransferThree.air")
                    import MahjongTransferThree
                    MahjongTransferThree.runTest({})
                
            
            vipOverLayer = poco("VipOverLayer")
            if vipOverLayer.exists():
                isPlaying = False
                
        return True

_instance = MahjongPlay()
runTest = _instance.runTest



